package com.sk.ccflower.lib.sockUtils.packet;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
 * @author eunwoogi
 * @since 2010. 02. 22.
 * TCP/IP 프로토콜 메시지 헤더 정의
 */
public class DataHeader {
	
	public static int HEADER_LENGTH = 48;
	
	public	char	version;
	public	char	type;
	public	char	status;
	public	char	pad;
	public	int		mid;
	public	int		uid;
	public	int		seq;
	public	int		len;
	public	int		ts;
	public	int		spare1;
	public	int		spare2;
	public	int		domain1;
	public	int		domain2;
	public	int		node1;
	public	int		node2;
	
	public DataHeader() {
		version	= '1';
		type	= 'R';
		status	= 'N';
		pad		= '\0';
		mid		= 0;
		uid		= 0;
		seq		= 0;
		len		= 0;
		ts		= (int) (System.currentTimeMillis() / 1000);
		spare1	= 0;
		spare2	= 0;
		domain1	= 0;
		domain2	= 0;
		node1	= 0;
		node2	= 0;
	}
	
	public void SetData( byte[] buf ) {
		ByteBuffer buffer;
		buffer = ByteBuffer.allocate(buf.length);
		buffer.order(ByteOrder.BIG_ENDIAN);
		buffer.put( buf );
		buffer.flip();
		
		version = (char)buf[0];
		type = (char)buf[1];
		status = (char)buf[2];
		pad = (char)buf[3];
		mid = buffer.getInt(4);
		uid = buffer.getInt(8);
		seq = buffer.getInt(12);
		len = buffer.getInt(16);
		ts = buffer.getInt(20);
		spare1 = buffer.getInt(24);
		spare2 = buffer.getInt(28);
		domain1 = buffer.getInt(32);
		domain2 = buffer.getInt(36);
		node1 = buffer.getInt(40);
		node2 = buffer.getInt(44);
	}
	
	public ByteBuffer GetByteBuffer() {
		ByteBuffer buffer = ByteBuffer.allocate(48);
		buffer.order(ByteOrder.BIG_ENDIAN);
		
		buffer.put( (byte)version );
		buffer.put( (byte)type );
		buffer.put( (byte)status );
		buffer.put( (byte)pad );
		buffer.putInt( mid );
		buffer.putInt( uid );
		buffer.putInt( seq );
		buffer.putInt( len );
		buffer.putInt( ts );
		buffer.putInt( spare1 );
		buffer.putInt( spare2 );
		buffer.putInt( domain1 );
		buffer.putInt( domain2 );
		buffer.putInt( node1 );
		buffer.putInt( node2 );
		
		return buffer;
	}
	
	public boolean IsValidHeader() {
		if ( version != '1' )
			return false;
		if ( len > 4096 )
			return false;
		if ( (type != 'P') && (type != 'R') && (type != 'I') )
			return false;
		
		return true;
	}
	
	public String getHeaderLog() {
		StringBuffer sb = new StringBuffer();
		sb.append("version : "+version);
		sb.append( "\n" );
		sb.append("type : "+type);
		sb.append( "\n" );
		sb.append("status : "+status);
		sb.append( "\n" );
		sb.append("pad : "+pad);
		sb.append( "\n" );
		sb.append("mid : "+mid);
		sb.append( "\n" );
		sb.append("uid : "+uid);
		sb.append( "\n" );
		sb.append("ts : "+ts);
		sb.append( "\n" );
		sb.append("seq : "+seq);
		sb.append( "\n" );
		sb.append("len : "+len);
		sb.append( "\n" );
		sb.append("spare1 : "+spare1);
		sb.append( "\n" );
		sb.append("spare2 : "+spare2);
		sb.append( "\n" );
		sb.append("domain1 : "+domain1);
		sb.append( "\n" );
		sb.append("domain2 : "+domain2);
		sb.append( "\n" );
		sb.append("node1 : "+node1);
		sb.append( "\n" );
		sb.append("node2 : "+node2);
		
		return sb.toString();
	}
	
}
